<template>
  <div class="icons-box">
    <swiper :options="swiperOption">
      <!-- 已经通过计算属性将图标进行分页，swiper-slide 对页码进行循环 -->
      <swiper-slide v-for="(page, index) of pages" :key="index">
        <!-- 对每页进行循环，取出图标信息 -->
        <div class="icon" v-for="item of page" :key="item.id">
          <div class="icon-img-box">
            <img class="icon-img" :src="item.iconUrl" alt="">
          </div>
          <p class="icom-info">{{ item.iconText }}</p>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
  export default {
    name: 'homeIcons',
    props: {
      iconList: Array
    },
    data (){
      return {
        swiperOption: {
          autoplay: false
        }
      }
    },
    computed: {
      pages (){
        const pages = []
        this.iconList.forEach((item, index) => {
          // 根据图标的下标来计算分页数
          const page = Math.floor(index / 8)
          if (!pages[page]) {
            pages[page] = []
          }
          pages[page].push(item)
        })
        return pages
      }
    }
  }
</script>

<style  lang="stylus" scoped>
  @import '~@S/varibles.styl'
  @import '~@S/mixins.styl'
  .icons-box >>> .swiper-container
    height: 0
    padding-bottom: 50%
  .icon
    float: left
    height: 0
    width: 25%
    padding-bottom: 25%
    overflow: hidden
    position: relative
    .icon-img-box
      position: absolute
      top: 0
      left: 0
      right: 0
      bottom: .4rem
      box-sizing: border-box
      padding: .1rem
      .icon-img
        height: 100%
        display: block
        margin: 0 auto
    .icom-info
      color: $texColor
      ellipsis()
      position: absolute
      left: 0
      right: 0
      bottom: 0
      line-height: .4rem
      height: .4rem
      text-align: center
</style>
